package com.edu.cbtu.dp01.service;
import com.edu.cbtu.dp01.constant.REnum;
import com.edu.cbtu.dp01.dao.StudentRepository;

import com.edu.cbtu.dp01.entity.Student;
import com.edu.cbtu.dp01.exception.RException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.awt.print.Pageable;
import java.util.List;
@Service
public class StudentService {
    @Autowired
    public StudentRepository studentRepository;


//    private final StudentRepository studentRepository;

    public StudentService(StudentRepository studentRepository) {
        this.studentRepository = studentRepository;
    }

    public Student saveStudent(Student student) {
        if (shouldUseManualId(student)) {
            // 手动ID模式
        } else {
            student.setId(null); // 自增模式
        }
        return studentRepository.save(student);
    }

    private boolean shouldUseManualId(Student student) {
        return student.getId() != null;
    }


    /**
     * 读所有学生列表
     *
     * @return List student
     */
    public List<Student> getAll() {

        return studentRepository.findAll();
    }

    public Page<Student> getAll(Pageable pageable){
        return studentRepository.findAll((org.springframework.data.domain.Pageable) pageable);
    }
    public Page<Student> getAll(Example<Student> example, org.springframework.data.domain.Pageable pageable){
        return studentRepository.findAll(example,pageable);
    }


    public Student findById(int id) {
        return studentRepository.findById(id).get();
    }

    /**
     * 新增学生
     *
     * @param student
     * @return
     */
    public Student add(Student student) {
        return studentRepository.save(student);
    }

    /**
     * 更新学生
     *
     * @param student
     * @return
     */
    public Student update(Student student) {
        return studentRepository.save(student);
    }

    /**
     * 删除学生
     *
     * @param id
     */
    public void delete(Integer id) {
        studentRepository.deleteById(id);
    }

    public Student validateUsernameAndPassword(String sno,String password) throws Exception{

        List<Student> students=studentRepository.findBySno(sno);
        if(students.size() > 0){

            Student student=students.get(0);
            if(student.getPassword().equals(password)){

                return student;
            }else{
                throw new RException(REnum.LOGIN_ERR);

            }
        }else{
            throw new RException(REnum.LOGIN_ERR);
        }
    }


}
//    /**
//     * 按id进行查询
//      * @param id 主键，整数
//     * @return
//     */
//    public student findById(Integer id){
//
//        return studentRepository.findById(id).orElse(null);
//    }
//
//    /**
//     * 按名字进行查询，like
//     * @param id 主键，整数
//     * @return
//     */
//    public List<student> findByName(String name){
//
//        return studentRepository.findByNameLike(name);
//    }
//
//
//    /**
//     * 按名字进行查询
//     * @param name
//     * @param password
//     * @return
//     */
//    public List<student> findByNameAndPassword(String name,String password){
//
//        return studentRepository.findByNameAndPassword(name,password);
//    }
//
//    public void insert(student student){
//        studentRepository.save(student);
//    }
//
//    public void updata(student student){
//        studentRepository.save(student);
//    }
//    public void delete(Integer id){
//        studentRepository.deleteById(id);
//    }
//}
